# 
# configuration.rb
# 
# Created on 12/09/2007, 16:33:34
# 

# Here is defined ARDataContainer, the object that will hold all user 
# supplied configuration and will generate defaults for all the missing options
AutoRest.ar_require_relative __FILE__, 'ardata_container.rb'

# Helpers to manipulate the ARDataContainer data in usefull ways
AutoRest.ar_require_relative __FILE__, 'configuration_helpers.rb'

module AutoRest
  
  # Factory method for getting a Resource Struct.
  def Resource params= {}
    returning(Struct::ARResource.new) do |reg|
      [:singular, :plural, :model, :name_prefix].each do |key|
        reg.send("#{key}=", params[key])
      end
    end
  end
  module_function :Resource
  
  # Factory method for getting a Helper Caller. Used when defining the fields
  # for each view.
  def HelperCall method_name
    Struct::ARHelperCall.new method_name
  end
  module_function :HelperCall

  module Configuration
    private 
    
    # Creates a new options container to pass to the user for configuration
    def prepare_options_container
      ARDataContainer.new
    end
    
    # --------------------------------------------------------------------------
    # OPTIONS HANDLER
    #   Give defaults to options not configured by the user 
    # --------------------------------------------------------------------------
    def proccess_options_container options
      # Here we begin to work with the Resource class instances that rails created
      # when loaded routes.rb 
      # (see rails_resources.rb in the plugin /lib/patches/rails folder)
     
      # The resource may show up in many contexts ie.: as child of many parents
      # I choose the resource with no name prefix.
      all_resources= AutoRest::RailsResources.of_controller(controller_name)
      
      if !all_resources || all_resources.empty?
        message= "AutoRest: No resources have been found for the controller #{controller_name}.\n" \
                 "Have you properly configured routes.rb?"
        raise StandardError.new(message)
      end
      
      # I need to select the resource that is not a nested resource ==> name_prefix == nil
      selected_resource= all_resources.select { |e| e.name_prefix == nil }
      
      if selected_resource && !selected_resource.empty?
        resource= selected_resource.first 
      else
        message= "AutoRest: Couldn't find resource for controller #{controller_name}\n" +
                 "Have you set the resource on routes.rb?\n" \
                 "If it is a nested controller, you should also add a route to it as root resource."
        raise StandardError.new(message) 
      end
      
      # All the controller parentship information is retrieved.
      resources_parents= AutoRest::RailsResources.resources_parents
      
      # Now check all user options, and provide defaults for options not configured.
      returning(options) do |opt| 
        opt.check_all_options :resource => resource, 
                              :resources_parents => resources_parents, 
                              :controller => self
      end
    end
    
  end
end
